package net.lzzy.practices.web.courses;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Massages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.services.CourseService;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author admin
 */
@RestController
@RequestMapping(ApiConstant.ROUTE_CHAPTER_ROOT)
@CrossOrigin
@Api("章节相关业务")
public class ChapterApiController {
    private final ChapterService service;
    private final UserService uService;
    private final CourseService cService;
    @Autowired
    public ChapterApiController(ChapterService service, UserService uService, CourseService cService) {
        this.service = service;
        this.uService = uService;
        this.cService = cService;
    }
    @ApiOperation("查询章节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<VmChapter> chapters=new ArrayList<>();
        service.getTeachersChapters(courseId).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true,chapters.size()+"",chapters);

    }
    @ApiOperation("查询学生章节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true),
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_CHAPTER_STUDENT)
    public ApiResult getStudentsChapters(@RequestParam String token,@RequestParam(name = "id") UUID courseId){
        List<VmChapter> chapters=new ArrayList<>();
        service.getStudentsChapters(courseId).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true,chapters.size()+"",chapters);
    }

    //获取参数
    public ApiResult addAndUpdate(UUID id, ChapterInfo info){
        String token=info.getToken();
        String name = info.getName();
        String desc=info.getDesc();
//    String cover=info.getCover();
        String courseId=info.getCid();
        boolean open=info.isOpen();

//进行完整性检查
        if (StringUtil.isEmpty(name)||
                StringUtil.isEmpty(desc)||
//        StringUtil.isEmpty(cover)||
                StringUtil.isEmpty(courseId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(),null);
        }
        Course course=cService.getById(UUID.fromString(courseId));
        if (course==null){
            return new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }


        Chapter chapter;
        boolean add = id ==null;
        if (add){
            chapter = new Chapter();
            chapter.setCourse(course);
        }else {
            chapter = service.getById(id);
            if (chapter==null){
                return new ApiResult(false,Massages.WRONG_ID.toString(), null);
            }
        }


        chapter.setName(name);
        chapter.setDescription(desc);
        chapter.setOpen(open);
        chapter.setUpdateTime(new Date());
        if (add){
            return new ApiResult(true,Massages.SUCCESS.toString(), VmChapter.create(service.add(chapter)));
        }else {
            Chapter updated=service.update(chapter,token);
            if (updated==null){
                return new ApiResult(false, Massages.WRONG_ID.toString(), null);
            }else {
                return new ApiResult(true,Massages.SUCCESS.toString(), VmChapter.create(updated));
            }
        }
    }
    @ApiOperation("添加章节")
    @ApiImplicitParam(name = "info",value = "章节信息",dataType = "ChapterInfo",dataTypeClass = ChapterInfo.class,paramType = "body", required = true)
    @PostMapping(ApiConstant.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info){
        return addAndUpdate(null,info);
    }
    @ApiOperation("更新章节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "info",value = "章节信息",dataType = "ChapterInfo",dataTypeClass = ChapterInfo.class,paramType = "body", required = true),
            @ApiImplicitParam(name = "id",value = "章节id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "path", required = true)
    })
    @PostMapping(ApiConstant.ROUTE_CHAPTER_PUT)
    public ApiResult update(@RequestBody ChapterInfo info, @PathVariable UUID id){
        return addAndUpdate(id,info);
    }
    @ApiOperation("更新章节状态")
    @ApiImplicitParam(name = "info",value = "章节信息",dataType = "ChapterOpenInfo",dataTypeClass = ChapterOpenInfo.class,paramType = "body", required = true)
    @PostMapping(ApiConstant.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info){
        String token = info.getToken();
        String strChapterId=info.getCid();
        boolean open= info.isOpen();
        if (StringUtil.isEmpty(token) || StringUtil.isEmpty(strChapterId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(), false);
        }
        Chapter chapter=service.openCloseChapter(UUID.fromString(strChapterId),open,token);
        if (chapter == null){
            return new ApiResult(false,Massages.NO_PERMISSION.toString(), false);
        }
        return new ApiResult(true,Massages.SUCCESS.toString(), chapter);
    }
    @ApiOperation("删除章节")
    @ApiImplicitParam(name = "info",value = "章节信息",dataType = "ChapterKeyInfo",dataTypeClass = ChapterKeyInfo.class,paramType = "body", required = true)
    @DeleteMapping(ApiConstant.ROUTE_CHAPTER_REMOVE)
    public ApiResult remove(@RequestBody ChapterKeyInfo info){
        String token = info.getToken();
        String strChapterId=info.getCid();
        String strCourseId=info.getCourseId();
        if (StringUtil.isEmpty(token) || StringUtil.isEmpty(strChapterId) || StringUtil.isEmpty(strCourseId)){
            return new ApiResult(false, Massages.MISS_ARGS.toString(), null);
        }
        try {
            UUID courseId=UUID.fromString(strCourseId);
            UUID chapterId = UUID.fromString(strChapterId);
            Massages msg=service.remove(chapterId,courseId,token);
            return new ApiResult(msg==Massages.SUCCESS,msg.toString(),null);
        }catch (Exception e){
            return  new ApiResult(false,Massages.WRONG_ID.toString(),null);
        }
    }
}
